import { apiFindPersonalRez,apiCancelRez } from '@/api/reservation'
import { useErrMessage, useInfoMessage } from '@/utils/message'

Page({
  data: {
    findRez: [],
    page: 1,
    pageSize: 10,
    hasMore: false,
    total: 0,
  },

  async FindPersonalRez() {
    try {
      
      const res = await apiFindPersonalRez({
        page: this.data.page,
        pageSize: this.data.pageSize,
        status: 0,
      });
      
      if (res.code === 200) {
        let newData = res.data.reservations || [];
        let total = res.data.total || 0;
        
        // 判断是否还有更多数据
        const hasMore =total > this.data.page * this.data.pageSize;
        
        this.setData({
          findRez: newData,
          hasMore: hasMore,
        });
      }
    } catch (error) {
      useErrMessage(this, '获取预约数据失败，请重试');
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    await this.FindPersonalRez();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // 防抖处理
    if (!this.data.hasMore) {
      return;
    }
    setTimeout(() => {
      this.setData({
        page: this.data.page + 1,
      })
      this.FindPersonalRez();
    }, 500);
  },
  
  // 处理取消预约
  handleCancelReservation(e) {
    const id = e.detail.id;
    apiCancelRez(id).then(res=>{
      if(res.code === 200){
        useInfoMessage(this,'取消预约成功')
        wx.redirectTo({
          url: '/pages/reservation/allRez/index',
        })
      }
    })
  }
})